import UIKit
import Flutter
import microcosm_log

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    testLogBeforeRegister()
    
    GeneratedPluginRegistrant.register(with: self)
    
    testLogAfterRegister()
    
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    
  func testLogBeforeRegister() {
      FlutterLog.v(tag: "AppDelegate", msg: "Before")
  }

  func testLogAfterRegister() {
      FlutterLog.v(tag: "AppDelegate", msg: "v")
      FlutterLog.d(tag: "AppDelegate", msg: "d")
      FlutterLog.i(tag: "AppDelegate", msg: "i")
      FlutterLog.w(tag: "AppDelegate", msg: "w")
      FlutterLog.e(tag: "AppDelegate", msg: "e")
      
      asyncLog()
      multiThreadLog()
  }
    
  func asyncLog() {
      DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + 2, execute: {
          FlutterLog.v(tag: "AppDelegate", msg: "v")
      })
  }
    
  func multiThreadLog(){
      for i in 0...10 {
          let seconds = Double(arc4random()%10)
          DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + seconds, execute: {
              FlutterLog.d(tag: "AppDelegate", msg: String(i))
          })
      }
  }
}
